<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Model\AdminUser;
use App\User;
use Auth;
use Toastr;
use DB,Session;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AuthController extends Controller
{
    public function login(){
        //如果已经登录，跳转到首页（未完成）
        if(Auth::guard('admin')->check()){
            return redirect('admin/index');
        }
        return view('admin.login');
    }

    // 执行登录
    public function dologin(Request $request)
    {
        $this->validate($request,[
            'username' => 'required',
            'password' => 'required'
        ],[
            'username.required' => '用户名不能为空',
            'password.required' => '密码不能为空',
        ]);

        //$data['username'] = $request->username;
        //$data['password'] = $request->password;
        $remember = $request->input('remember' , false);

        $user = AdminUser::where('username', $request->input('username'))->where('password', md5($request->input('password')))->first();
//dd($request->all(),$user);
        if(empty($user)){
            Toastr::error('账号/密码错误');
            return back();
        }
        Auth::guard('admin')->login($user);
        return redirect('/admin/index');

    }


    // 退出登录
    public function getlogout()
    {
        Auth::guard('admin')->logout();
        return redirect('/admin/login');
    }


    public function password_edit()
    {
        $id = Auth::guard('admin')->user()->id;
        $data = adminUser::where('id',$id)->first();
        return view('admin.system_manage.edit',['data'=>$data]);
    }

    public function password_update(Request $request)
    {
        $this->validate($request,[
            'password' => 'required|min:6',
        ],[
            'password.required' => '密码不能为空',
            'password.min' => '密码不能少于6位',
        ]);

        $id = $request->id;
        $password = $request->password;

        $res = adminUser::where('id',$id)->update(['password'=>$password]);

        if(!empty($res)){
            Toastr::success('修改成功');
            return redirect('admin/case/index');
        }else{
            Toastr::error('修改失败');
            return back();
        }
    }
}
